package com.hulk.other;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class 多线程并发测试 {
	public static void main(String[] args) {
		int count = 500;
		CyclicBarrier cyclicBarrier = new CyclicBarrier(count);
		ExecutorService executorService = Executors.newFixedThreadPool(count);
		for (int i = 0; i < count; i++)
			executorService.execute(new 多线程并发测试().new Task(cyclicBarrier));
		executorService.shutdown();
		int i = 0;
		while (!executorService.isTerminated()) {
			
			try {
				Thread.sleep(1000);
				System.out.println(i);
				i++;
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public class Task implements Runnable {
		private CyclicBarrier cyclicBarrier;

		public Task(CyclicBarrier cyclicBarrier) {
			this.cyclicBarrier = cyclicBarrier;
		}

		@Override
		public void run() {
			try { // 等待所有任务准备就绪
				cyclicBarrier.await(); // 测试内容
				try {
					String host = "http://localhost:8090/invite/touchGold?";
					String para = "userId=146966974683864370&activityId=73&source=1";
					System.out.println(host + para);
					URL url = new URL(host);// 此处填写供测试的url
					HttpURLConnection connection = (HttpURLConnection) url.openConnection();
					// connection.setRequestMethod("POST");
					// connection.setRequestProperty("Proxy-Connection",
					// "Keep-Alive");
					connection.setDoOutput(true);
					connection.setDoInput(true);
					PrintWriter out = new PrintWriter(connection
							.getOutputStream());
					out.print(para);
					out.flush();
					out.close();
					BufferedReader in = new BufferedReader(
							new InputStreamReader(connection
									.getInputStream()));
					String line = "";
					String result = "";
					while ((line = in.readLine()) != null) {
						result += line;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
